﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DNA.Mvc
{
    public static class PackageExtensions
    {
        /// <summary>
        /// Resolve the physical path and combine with the specified path.
        /// </summary>
        /// <param name="package"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ResolvePhysicalPath(this Package package,string path="")
        {
            return System.IO.Path.GetDirectoryName(HttpContext.Current.Server.MapPath(package.Url))+"\\"+path;
        }

        public static string ResolveFileName(this Package package)
        {
            return HttpContext.Current.Server.MapPath(package.Url);
        }

        /// <summary>
        /// Resolve the virual path that contains the package.
        /// </summary>
        /// <param name="package"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ResolveUrl(this Package package,string path)
        {
            var vpath=VirtualPathUtility.ToAbsolute( VirtualPathUtility.GetDirectory(package.Url));
            if (vpath.EndsWith("/"))
                return vpath+path;
            else
                return vpath+"/"+path;
        }
    }
}